package com.ssyc.student.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.SpKeys;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.SnackbarUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.TypeTextView;
import com.ssyc.student.R;
import com.ssyc.student.adapter.ChoosePetGvAdapter;
import com.ssyc.student.bean.ChoosePetInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生端--选择宠物界面
 * Created by Administrator on 2018/4/18 0018.
 */

public class StudentChooserPetActivity extends BaseActivity implements View.OnClickListener {
    //主布局
    private LinearLayout llMain;
    //显示的文字
    private TypeTextView tvChoosePet;
    //显示的宠物的GridView
    private GridView mGv;
    //确定按钮
    private Button btOk;
    //没选中的宠物资源
    private int[] unSelectedIds = {R.drawable.st_dog_unselect, R.drawable.st_cat_unselect,
            R.drawable.st_monster_unselect, R.drawable.st_panda_unselect};

    //选中的宠物资源
    private int[] selectedIds = {R.drawable.st_dog_select,
            R.drawable.st_cat_select,R.drawable.st_monster_select, R.drawable.st_panda_select};


    //用于选定宠物弹框后需要显示的宠物资源
    private int[] flickerIds = {R.drawable.student_flicker_dog,R.drawable.student_flicker_cat,R.drawable.student_flicker_monster
            , R.drawable.student_flicker_panda};


    //用于控制打字机是否打印完毕的标记
    private boolean isPrintFinished;

    //用于展示宠物的列表的数据集合
    private List<ChoosePetInfo> datas;
    private ChoosePetGvAdapter adapter;
    //记录选中宠物的位置
    private int selectedPos = -1;
    //闪光帧动画对象
    private AnimationDrawable animationDrawable;

    //用于跳转的Handler对象
    private Handler handler = new Handler();

    //返回按钮
    private ImageView iv_back;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_choose_pet;
    }

    @Override
    protected void init() {
        showContent();
        //初始化视图
        initView();
        //创建数据
        initData();
        //设置展示宠物里列表的数据
        initGv();
    }


    /**
     * 初始化视图的方法
     */
    private void initView() {

        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);

        llMain = findViewById(R.id.ll_main);
        tvChoosePet = findViewById(R.id.tv_choose_pet);
        tvChoosePet.start(getResources().getString(R.string.st_choose_pt));
        tvChoosePet.setOnTypeViewListener(new TypeTextView.OnTypeViewListener() {
            @Override
            public void onTypeStart() {

            }

            @Override
            public void onTypeOver() {
                //打字机效果打印完毕后改变标记值
                isPrintFinished = true;
                if (selectedPos == -1) {
                    //还没有选择宠物
                    btOk.setBackgroundResource(R.drawable.student_bt_ok_unselect);
                    btOk.setEnabled(false);
                } else {
                    //已经选择宠物
                    btOk.setEnabled(true);
                    btOk.setBackgroundResource(R.drawable.student_bt_ok_select);
                }

            }
        });
        mGv = findViewById(R.id.mgv);
        btOk = findViewById(R.id.bt_OK);
        btOk.setEnabled(false);
        btOk.setOnClickListener(this);

    }

    /**
     * 创建数据的方法
     */
    private void initData() {
        datas = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            ChoosePetInfo info = new ChoosePetInfo(false, unSelectedIds[i], selectedIds[i]);
            datas.add(info);
        }
    }

    /**
     * 设置展示宠物里列表的数据
     */
    private void initGv() {
        adapter = new ChoosePetGvAdapter(this, datas, R.layout.student_gv_choose_pet);
        mGv.setAdapter(adapter);
        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int positon, long l) {
                //记录选中宠物的位置
                selectedPos = positon;
                //更新选中的位置
                adapter.updateState(positon);

                //点击同时打印结束,改变高亮按钮状态,否则继续不让点击并且灰色显示按钮
                if (isPrintFinished) {
                    btOk.setEnabled(true);
                    btOk.setBackgroundResource(R.drawable.student_bt_ok_select);
                } else {
                    btOk.setBackgroundResource(R.drawable.student_bt_ok_unselect);
                    btOk.setEnabled(false);
                }
            }
        });
    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bt_OK) {
            //确定按钮
            doChooseAction();
        } else if (id == R.id.iv_back) {
            finish();
        }
    }


    /**
     * 点击确定按钮做提交处理
     */
    private void doChooseAction() {
        if (selectedPos == -1) {
            //表示还没有选择宠物
            SnackbarUtil.ShortSnackbar(llMain, "您还没有选择宠物", 0).show();
        } else {
            //将选中的值保存到Sp中,注意,这里保存的值对应的是0,1,2,3

            if(selectedPos==0){
                SPUtil.put(StudentChooserPetActivity.this, SpKeys.PETTYPE, 1);
            }else if(selectedPos==1){
                SPUtil.put(StudentChooserPetActivity.this, SpKeys.PETTYPE, 2);
            }else if(selectedPos==2){
                SPUtil.put(StudentChooserPetActivity.this, SpKeys.PETTYPE, 3);
            }else if(selectedPos==3){
                SPUtil.put(StudentChooserPetActivity.this, SpKeys.PETTYPE, 4);
            }

            showChoosePetDialog();
        }
    }


    /**
     * 弹出已经选择宠物的Dialog
     */
    private void showChoosePetDialog() {
        PopUpManager.showPopWithMatch(this, R.layout.st_dialog_choose_pet, 0.3f, this, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //外层的闪光灯
                ImageView lvOut = view.findViewById(R.id.lv_out);
                //宠物的图片
                ImageView lvPet = view.findViewById(R.id.lv_pet);
                lvPet.setImageResource(flickerIds[selectedPos]);
                //对整体外层的背景做帧动画处理
                lvOut.setBackgroundResource(R.drawable.st_frame_pet_anim);
                animationDrawable = (AnimationDrawable) lvOut.getBackground();
                animationDrawable.start();
                //对宠物本身做缩放,选择,透明的组合动画效果
                //透明度动画
                ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(lvPet, "alpha", 0f, 1f);
                //旋转动画
                ObjectAnimator rotationAnim = ObjectAnimator.ofFloat(lvPet, "rotation", 0f, 360f);
                //缩放
                ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(lvPet, "scaleX", 0f, 1f);
                ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(lvPet, "scaleY", 0f, 1f);
                //组合动画集
                AnimatorSet set = new AnimatorSet();
                set.play(scaleXAnim).with(scaleYAnim).with(alphaAnim).with(rotationAnim);
                set.setDuration(2500);
                set.setInterpolator(new BounceInterpolator());
                set.start();
                //动画的监听
                set.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {

                        //使用Handler进行3秒后跳转到下一个界面
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                //释放动画对象
                                if (null != animationDrawable && animationDrawable.isRunning()) {
                                    animationDrawable.stop();
                                    animationDrawable = null;
                                }
                                PopUpManager.dismiss();
                                //跳转页面
                                UiUtils.startActivity(StudentChooserPetActivity.this, StudentPetGenderChooseActivity.class);

                            }
                        }, 800);
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {

                    }
                });
            }
        });
    }


    /**
     * 接受事件的方法
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        //关闭页面
        if (null != busInfo && StudentEditPetNameActivity.PETFINISH.equals(busInfo.vpScoll)) {
            finish();
        }
    }

    /**
     * 生命周期中取消打字机效果
     */
    @Override
    protected void onDestroy() {
        if (null != tvChoosePet) {
            tvChoosePet.stop();
        }

        //释放动画对象
        if (null != animationDrawable && animationDrawable.isRunning()) {
            animationDrawable.stop();
            animationDrawable = null;
        }
        //释放handler,防止内存泄漏
        if (null != handler) {
            handler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }
}
